/**
 * Project:   dtu.fmes.googleloon
 * File:      ChargeBatterySimulationLink.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright: Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * Date:      Oct 23, 2013
 */

package dtu.fmes.loon.simulation.processors;

import java.util.concurrent.TimeUnit;

import dtu.fmes.loon.model.Balloon;
import dtu.fmes.loon.model.ConstantBalloonValues;
import dtu.fmes.loon.utils.SimulationStateUtils;

public class ChargeBatterySimulationLink extends SimulationLink {

	@Override
	public void run() {
		ConstantBalloonValues cbv = css.getConstantBalloonValues();

		boolean day = SimulationStateUtils.isDay(css.getSimulationState());

		long chargeTime = css.getSimulation().getSimulationInterval();
		Double chargeRate = day ? cbv.getSolarPanel().getDayChargingSpeed() : cbv.getSolarPanel().getNightChargingSpeed();
		Double chargeAmount = chargeTime * chargeRate / TimeUnit.HOURS.toSeconds(1);

		for (Balloon b : css.getBalloons()) {
			// Only charge balloons, which can be revived.
			if (b.isCanBeRevived()) {
				b.setCurrentBatteryCapacity((b.getCurrentBatteryCapacity() + chargeAmount));

				// Make sure that current capacity will not exceed maximum
				// capacity.
				if (b.getCurrentBatteryCapacity() > cbv.getBattery().getMaximumCapacity()) {
					b.setCurrentBatteryCapacity(cbv.getBattery().getMaximumCapacity());
				}
			}
		}

	}
}
